#include "longnumber.h"

LongNumber::LongNumber(QString str) {
  for (int i = 0; i < str.length(); i++) {
    list.prepend(str.at(i).digitValue());
  }
}

int LongNumber::numOfDigits() const {
  return list.size();
}

QString LongNumber::toString() const {
  QString result = "";
  foreach (short c, list) {
    result.prepend(QString::number(c));
  }
  return result;
}

LongNumber LongNumber::operator + (const LongNumber &number) {

  LongNumber result("");

  QLinkedListIterator<short> i(list);
  QLinkedListIterator<short> j(number.list);

  short carry = 0;

  while (i.hasNext() || j.hasNext()) {

    short sum = carry;

    if (i.hasNext()) {
      sum += i.next();
    }
    if (j.hasNext()) {
      sum += j.next();
    }

    result.list.append(sum % 10);
    carry = sum / 10;
  }

  if (carry) {
    result.list.append(carry);
  }
  return result;
}

LongNumber LongNumber::operator +=(const LongNumber &number) {
  list = (*this + number).list;
  return *this;
}
